package com.example.wordbook;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class AddFragment extends Fragment {
    private Button ButtonSubmit;
    private EditText EditTextEnglish, EditTextChinese;
    private WordViewModel wordViewModel;


    public AddFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add, container, false);
    }
       //LayoutInflater是用来找res/layout/下的xml布局文件，并且实例化
       //对于一个没有被载入或者想要动态载入的界面，都需要使用LayoutInflater.inflate()来载入
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final FragmentActivity activity = requireActivity();
        wordViewModel = ViewModelProviders.of(activity).get(WordViewModel.class); //viewmodel创建方法（被弃用？）
        ButtonSubmit = activity.findViewById(R.id.buttonSubmit);
        EditTextEnglish = activity.findViewById(R.id.editTextEnglish);
        EditTextChinese = activity.findViewById(R.id.editTextChinese);
        ButtonSubmit.setEnabled(false);//有效信息不足时，按键设置为灰色状态，刚开始时，为不启用状态
        //定义了添加单词界面的功能
        //进入界面后，将输入框聚焦在英语输入框上，并弹出键盘
        EditTextEnglish.requestFocus();//光标获取焦点
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(EditTextEnglish,0);//强制显示键盘，两个参数为（1. 关联的View；2. flag）
         //同时再使用该方法之前，view需要获得焦点，可以通过requestFocus()方法来设定
        //制作edit监听器
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
           //若输入框两个都不为空，设置添加按钮可用
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String english = EditTextEnglish.getText().toString().trim();
                String chinese = EditTextChinese.getText().toString().trim();
                ButtonSubmit.setEnabled(!english.isEmpty() && !chinese.isEmpty());//只要两者都不是空
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        //添加到edit中
        EditTextEnglish.addTextChangedListener(textWatcher);
        EditTextChinese.addTextChangedListener(textWatcher);


        //点击添加按钮后，通过WordViewModel将数据写入数据库，并通过导航回到主界面
        ButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = EditTextEnglish.getText().toString().trim();
                String chinese = EditTextChinese.getText().toString().trim();
                Word word = new Word(english,chinese);
                wordViewModel.insertWords(word);//数据添加
                NavController navController = Navigation.findNavController(v);
                //button触发方法中添加键盘回缩方法
                navController.navigateUp();
                //返回界面时隐藏键盘的操作
                InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(),0);//两个参数 （1. 关联的View；2. flag）
            }
        });




    }
}
